<template>
  <div>
    <el-row :gutter="20">

      <!-- 搜索表单 -->
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
        <el-form-item label="编号" prop="categoryId">
          <el-input
            v-model="queryParams.categoryId"
            placeholder="请输入类别编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="名称" prop="categoryName">
          <el-input
            v-model="queryParams.categoryName"
            placeholder="请输入类别名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据操作按钮 -->
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" icon="el-icon-edit" size="small" :disabled="single" @click="handleUpdate">修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="small" :disabled="single" @click="handleDelete">删除
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column prop="categoryId" label="类别编号" align="center" width="100" />
        <el-table-column prop="categoryName" label="类别名称" align="center" />
        <!--el-table-column prop="createTime" label="创建日期" width="200" align="center"></el-table-column-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页加载 -->
      <el-pagination
        :current-page="queryParams.pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-row>

    <!-- 新增/修改对话框 默认不显示 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类别名称" prop="categoryName">
          <el-input v-model="form.categoryName" />
        </el-form-item>
        <!--el-form-item label="选择类别" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择">
            <el-option v-for="item in categoryList" :key="item.categoryId" :label="item.categoryName"
              :value="item.categoryId" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker v-model="form.createTime" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.statue">
            <el-radio label="1">正常</el-radio>
            <el-radio label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item-->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCategoryByPage,
  delCategory,
  getCategory,
  updateCategory,
  addCategory
} from '@/api/pos/category'
export default {
  name: 'Category',
  data() {
    return {
      // 表格数据
      categoryList: [],
      // 遮罩层
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        categoryId: null,
        categoryName: null
      },
      form: {
        categoryName: null,
        categoryId: null
        // createTime: '',
        // statue: ''
      },
      rules: {
        categoryName: [{
          required: true,
          message: '请输入类别名称',
          trigger: 'blur'
        }],
        categoryId: [{
          required: true,
          message: '请选择类别ID',
          trigger: 'change'
        }]
      },
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 总页数
      total: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      // listAll().then(response => {
      //   this.categoryList = response.data;
      // })
      this.loading = true
      // 结果赋值this.dataList
      listCategoryByPage(this.queryParams).then(response => {
        this.categoryList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.getList()
    },

    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    /* submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$alert(this.form)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },*/
    /** 表单重置 */
    resetForm() {
      // 可进行数据初始化工作
      this.form = {
        categoryName: null,
        categoryId: null
        // createTime: '',
        // statue: ''
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.categoryId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm()
      this.open = true
      this.title = '添加数据'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm()
      const categoryId = row.categoryId || this.ids
      getCategory(categoryId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改数据'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const categoryId = row.categoryId || this.ids
      this.$confirm('是否确认删除ID为"' + categoryId + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return delCategory(categoryId)
        })
        .then(() => {
          this.getList() // 刷新数据
          this.$message.success('删除成功')
        })
        .catch(() => {})
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.categoryId != null) {
            updateCategory(this.form).then(response => {
              this.$message.success(response.msg)
              this.getList()
              this.cancel()
            })
          } else {
            addCategory(this.form).then(response => {
              this.$message.success(response.msg)
              this.getList()
              this.cancel()
            })
          }
        }
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.title = ''
      this.resetForm()
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    }
  }
}
</script>
